//
//  AppDelegate.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/15.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        self.setupAppearance()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //设置跟控制器
        window?.rootViewController = defaultViewController()
        //TODO: 测试欢迎界面
//        window?.rootViewController = LJWelcomeViewController()
        //TODO: 测试新特性界面
//        window?.rootViewController = LJNewFeatureViewController()
        
        window?.makeKeyAndVisible()
        return true
    }
    
    /// 切换根控制的方法
    func swtichRootViewController(isMain: Bool){
        window?.rootViewController = isMain ? LJMainViewController() : LJWelcomeViewController()
    }
    
    /// 确定跟控制器
    private func defaultViewController() -> UIViewController{
        if !LJUserAccount.isUserLogin{
            return LJMainViewController()
        }
        return isNewVerson() ? LJNewFeatureViewController() : LJWelcomeViewController()
    }
    private func isNewVerson() -> Bool{
        // 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print(currentVersion)
        //获得上次版本
        let sandBoxVersionKey = "sandBoxVersionKey"
        let sanBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxVersionKey)
        print(sanBoxVersion)
        
        // 保存最新版本
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandBoxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        return currentVersion != sanBoxVersion
        
    }
    /// 设置全局的View
    func setupAppearance(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

}

